package qdu.rg012.srfdraw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * SurfaceView 常用来进行自定义绘制，其绘制自有线程不会与主UI线程冲突，另外SurfaceView中的绘制使用双缓冲
 *SurfaceHolder是对SurfaceView的处理工具（一般并不能直接操控SurfaceView，android提供了SurfaceHolder作为操控工具）
 * 具体是通过 SurfaceHolder中的Callback回调来进行自定以内容
 */

public class Mysrfview extends SurfaceView implements SurfaceHolder.Callback {
    int cx= 0,cy = 260, radius = 60;
    int view_w, view_h;
    boolean isDraw = false;//该变量控制绘制的开关，避免线程同步中可能产生的canvas的空指针
    public Mysrfview(Context context) {
        super(context);
        SurfaceHolder mholder = this.getHolder();//以SurfaceView调用该函数，获取到当前SurfaceView专用的holder
        mholder.addCallback(this);//以 SurfaceHolder.Callback身份作为实参，将该类中重写的回调绑定到刚才get到的holder中
                                  //这样callback回调就可以作用到holder对应的surfaceview上。
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        //启动绘制线程
        isDraw = true;
        Log.i("mytag", "surfacecreate");
        new Thread(){
            public void run() {
                int i = 0;
                while(isDraw){
                //if(isDraw){
                    Canvas c = null;
                    c = holder.lockCanvas();//获取可绘制的画布
                    try{
                        if(c != null) {
                            refreshUi(c);
                            Paint p = new Paint();
                            p.setColor(Color.RED);
                            p.setTextSize(55f);
                            c.drawText("i:" + i, i*50f, 350f, p);
                            i++;
                        }
                    }catch(Exception e){
                        e.printStackTrace();
                    }finally {
                        holder.unlockCanvasAndPost(c);
                    }
                }
            }
        }.start();

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        isDraw = false;
        Log.i("mytag", "surfacedestroyed");
    }
    public void refreshUi(Canvas c){
        view_w = c.getWidth(); view_h = c.getHeight();
        //Log.i("mytag","w:" + view_w + ", h:" + view_h);
        if(Suract.flag){
            cx += 10;
            if(cx >= /*this.getWidth()*/ view_w + radius/2)
                cx = 0 - radius/2;
        }
        Paint bgp = new Paint();//绘制图形时每一个图形元素最好有一个独立的paint对象
        int oric = bgp.getColor();
        //c.drawColor(Color.YELLOW);
        bgp.setColor(Color.YELLOW);
        //c.drawRect(0,0,view_w,view_h,bgp);
        bgp.setColor(oric);

        Paint p = new Paint();
        p.setColor(Color.RED);
        p.setStyle(Paint.Style.FILL);
        c.drawCircle(cx,cy,radius,p);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
